(function($){
	
	var defaults = {
		maxfiles: 6,
		maxfilesize: 2000000,
		dir : '',
		module : ''
	}
	
	$.fn.dropfile = function(params){
		
		if(params){$.extend(defaults,params);}
	
		this.each(function(){
			$(this).bind({
				dragenter : function(e){
					e.stopPropagation();
					e.preventDefault();
				},
				draghover : function(e){
					e.stopPropagation();
					e.preventDefault();
					$(this).addClass('outputhover');
				},
				dragleave : function(e){
					e.stopPropagation();
					e.preventDefault();
					$(this).removeClass('outputhover');
				},
			});
			this.addEventListener('drop', function(e){
				e.stopPropagation();
				e.preventDefault();
				init_response();
				files = e.dataTransfer.files;
				upload($(this).attr('id'), files, $(this));
			}, false);
		});
		
		function upload(id, files, area) {
			var ln = files.length;
			var index = ln;
			var file;
			var filename;
			if (ln <= defaults.maxfiles && area.find(".thumb").length < defaults.maxfiles) {
				run();
				function run() {
					$(".response .text").html("");
					index--;
					file = files[index];
					filename = file.name;
					if (typeof filename == 'undefined') {
						filename = file.fileName;
					}
					var xhr = new XMLHttpRequest();
					if (file.type == "image/jpeg" || file.type == "image/png" || file.type == "image/gif") {
						if (xhr.upload && file.size <= defaults.maxfilesize) {
							$(".response .text").html("Upload du fichier <span class=black>"+filename+"</span> en cours...");
							$('.progress .int').width('0px');
							xhr.open('POST', 'index.php?controller=BackOffice&method=ajaxMethod', true);
							xhr.setRequestHeader("X-FILE-NAME", filename);
							xhr.setRequestHeader("X-ID", id);
							xhr.setRequestHeader("X-DIR", defaults.dir);
							xhr.send(file);
							if (index == ln-1) {
								area.find('p').remove();
							}
							area.append('<div class=progress><div class=int></div></div>');
							xhr.upload.addEventListener('progress', function(e){
								if (e.lengthComputable) {
									var per = Math.round(e.loaded/e.total*100)+'%';
									$('.progress .int').width(per);
								}
							}, false);
							xhr.addEventListener('load', function(e){
								$(".response .text").html("Upload effectu&eacute");
								var path = location.href.match(/^.*\//)+'datas/'+defaults.dir;
								var img = path+id+'/'+filename;
								var returnDisplay = "<div class=\"thumb "+defaults.module+"\" style=\"background-image:url('"+img+"');\"><a href="+img+" target=\"_blank\"></a></div><a class=\"delete-img\" href=# onClick=\"delete_img('"+img+"', '"+defaults.module+"');\"></a>";
								area.find('.progress').remove();
								area.append(returnDisplay);
								if (index != 0) {
									run();
								}
								else {
									kill_response();
								}
								var json = $.parseJSON(e.target.responseText);
								if (json.error) {
									//none
								}
							}, false);
						}
						else {
							var error = "Fichier <span class=black>"+filename+"</span> trop lourd";
							setError(error);
						}
					}
					else {
						var error = "Type du fichier <span class=black>"+filename+"</span> non support&eacute";
						setError(error);
					}
				}
				function setError(error) {
					$(".response .text").html(error);
					//if current item isn't the last file in the queue
					if (index != 0) {
						run();
					}
					else {
						//if there are much items than one in the queue
						if (ln != 1) {
							reload_view("reloadnews", "#newsInclude");
						}
						else {
							setTimeout(kill_response(), 1500);
						}
					}
				}
			}
			else {
				$(".response .text").html("Nombre de fichiers maximum atteind");
				setTimeout(kill_response(), 1500);
			}
		}
		
	}
		
})(jQuery);